package com.tengits.scanner;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

public class SilenceInstallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // 接收安装广播
        Log.e("MonitorService", intent.getAction());
        if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
            startApp(context);
        }
    }

    private boolean isRunningForeground (Context context) {
        ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
        String currentPackageName = cn.getPackageName();
        if(!TextUtils.isEmpty(currentPackageName) && currentPackageName.equals(context.getPackageName())) {
            return true ;
        }
        return false ;
    }

    /**
     * 启动app
     */
    public static void startApp(Context context) {
        /**
         * 启动service
         */
//        Intent intent1 = new Intent();
//        intent1.setPackage("×××.×××"); //设置被执行启动操作app包名
//        intent1.setAction("×××.×××"); //设置被执行启动操作app中service的自定义intent
//        context.startService(intent1);

        /**
         * 启动activity
         */
        Intent intent2= new Intent();
        ComponentName componentName = new ComponentName(
                "com.tengits.mqttmonitor",  //被执行启动操作app的包名
                "com.tengits.mqttmonitor.MainActivity");   //被执行启动操作app的activity包路径
        intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//
        intent2.setComponent(componentName);
        context.startActivity(intent2);
    }
}
